﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

namespace FYPHP
{
    public partial class MemberReservationManage : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
               
                    SqlCommand cmdProductReservation;
                    SqlDataReader dtrProductReservation;
                    conn.Open();

                    cmdProductReservation = new SqlCommand("SELECT * FROM Reservation r , Products p WHERE r.p_id=p.p_id AND r.u_id=@UID ", conn);
                    cmdProductReservation.Parameters.AddWithValue("@UID", Session["ID"].ToString());
                    dtrProductReservation = cmdProductReservation.ExecuteReader();

                    ItemList.DataSource = dtrProductReservation;
                    ItemList.DataBind();

                    dtrProductReservation.Close();
                    conn.Close();

                
            }
            catch (SqlException ex)
            {
                MessageBox(ex.Message);
            }
        }

        protected void ItemList_ItemCommand(Object Sender, RepeaterCommandEventArgs e)
        {
     

           if (e.CommandName == "DeleteProductReservation")
            {
                int pid = Convert.ToInt32(e.CommandArgument.ToString());
                SqlCommand dltProduct;
                conn.Open();

                dltProduct = new SqlCommand("DELETE FROM Reservation WHERE p_id=@PID", conn);
                dltProduct.Parameters.AddWithValue("@PID", pid);
                dltProduct.ExecuteNonQuery();
               
                MessageBox("Delete successfully!");
                Redirect("ProductReservation.aspx");
            }
        }

        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

        private void Redirect(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language=\"javascript\">window.location='" + msg + "';</script>";
            Page.Controls.Add(lbl);
        }
    }
}